package com.caiyi.financial.nirvana.sms.service;

import com.caiyi.financial.nirvana.sms.base.service.BaseService;
import com.caiyi.financial.nirvana.sms.entity.SmsAuditBatch;
import com.caiyi.financial.nirvana.sms.entity.SmsBatchMsg;
import com.caiyi.financial.nirvana.sms.enums.ReturnCodeEnum;
import com.caiyi.financial.nirvana.sms.mapper.SmsBatchMsgMapper;
import com.caiyi.financial.nirvana.sms.querybean.PageBean;
import com.caiyi.financial.nirvana.sms.querybean.QueryBatchMsgBean;
import com.caiyi.financial.nirvana.sms.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.youyu.common.utils.YyAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SmsBatchMsgService extends BaseService<SmsBatchMsg> {

    @Autowired
    private SmsBatchMsgMapper smsBatchMsgMapper;

    public PageBean<SmsAuditBatch> getBatchMsgByParams(QueryBatchMsgBean bean) {
        YyAssert.isFalse(bean.getStartTime() == null, ReturnCodeEnum.PARAMS_ERROR.code, "请输入开始时间!");
        YyAssert.isFalse(bean.getStatus() == null, ReturnCodeEnum.PARAMS_ERROR.code, "请输入审核状态!");
        if (bean.getStatus() != 1 && bean.getStatus() != 2 && bean.getStatus() != 3) {
            YyAssert.isFalse(true, ReturnCodeEnum.PARAMS_ERROR.code, "审核状态格式错误!");
        }
        if (null == bean.getEndTime()) {
            bean.setEndTime(new Date());
        }
        if (DateUtil.getDateDiffer(bean.getStartTime(), bean.getEndTime()) > 7) {
            YyAssert.isFalse(true, ReturnCodeEnum.PARAMS_ERROR.code, "开始时间和结束时间不能大于7天");
        }
        PageHelper.startPage(bean.getPageNum(), bean.getPageSize());
        List<SmsAuditBatch> list;
        if (bean.getStatus() == 2) {
            list = smsBatchMsgMapper.selectBatchMsgListByParams(bean);
        } else {
            list = smsBatchMsgMapper.selectAuditByParams(bean);
        }
        PageBean<SmsAuditBatch> pageBean = new PageBean<>(list);
        return pageBean;
    }
}
